{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 加一\n",
    "\n",
    "![Asked by LinkedIn](https://img.shields.io/badge/Asked%20by-LinkedIn-important?style=for-the-badge&logo=linkedin)\n",
    "\n",
    "&emsp;&emsp;给定一个非空数组，其中每个元素代表一个非负整数。\n",
    "\n",
    "&emsp;&emsp;请为数组$+1$。\n",
    "\n",
    "&emsp;&emsp;最高有效位在数组的前面，且数组中每个元素只有一位数字。此外，数组不包含前导的$0$，除非数组整体值为$0$。\n",
    "\n",
    "```text\n",
    " In[1]: [2, 3, 4]\n",
    "Out[1]: [2, 3, 5]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution(object):\n",
    "    def plusOne(self, digits):\n",
    "        upgrade = 1\n",
    "        for i in range(len(digits) - 1, -1, -1):\n",
    "            (upgrade, digits[i]) = (\n",
    "                (digits[i] + upgrade) // 10, (digits[i] + upgrade) % 10\n",
    "            )\n",
    "        if upgrade > 0:\n",
    "            digits.insert(0, upgrade)\n",
    "        return digits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 5]\n",
      "[3, 0, 0]\n",
      "[1, 0, 0, 0]\n"
     ]
    }
   ],
   "source": [
    "solution = Solution()\n",
    "print(solution.plusOne([2, 3, 4]))\n",
    "print(solution.plusOne([2, 9, 9]))\n",
    "print(solution.plusOne([9, 9, 9]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
